package check.md5;

import java.security.MessageDigest;

public class MessageDigestAdapterImpl extends MessageDigestAdapter {

	protected byte[] digestByte;
	protected String hexCheckSum;

	public MessageDigestAdapterImpl(MessageDigest messageDigest) {
		super(messageDigest);
	}

	public byte[] digest() {
		digestByte = messageDigest.digest();
		bytesToHex();
		return digestByte;
	}

	public byte[] digest(byte[] b) {
		digestByte = messageDigest.digest(b);
		bytesToHex();
		return digestByte;
	}
	
	public byte[] digest(String s) {
		return digest(s.getBytes());
	}

	public String getHexString() {
		return hexCheckSum;
	}

	public void update(byte b) {
		messageDigest.update(b);
	}

	public void update(byte[] b) {
		messageDigest.update(b);
	}
	
	public void update(String s) {
		update(s.getBytes());
	}

	public static String bytesToHex(byte[] b) {
		char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
		StringBuffer buf = new StringBuffer();
		for (int j = 0; j < b.length; j++) {
			buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
			buf.append(hexDigit[b[j] & 0x0f]);
		}
		return buf.toString();
	}

	private String bytesToHex() {
		return hexCheckSum = bytesToHex(digestByte);
	}
}
